package com.jams.goodox.activity.mine.bill;

import android.app.AlertDialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.adapter.CustomFragmentPagerAdapter;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;

/**
 * Created by Jin on 2016/7/13.
 * Description 我的账单
 */
public class MineBillActivity extends BaseActivity {

	@Bind(R.id.tabLayout)
	TabLayout tabLayout;
	@Bind(R.id.viewPager)
	ViewPager viewPager;

	private MineBillFragment fragment1;
	private MineBillFragment fragment2;
	private MineBillFragment fragment3;
	private MineBillFragment fragment4;
	private List<Fragment> fragmentList;

	public static final String BILL_TITLE = "BILL_TITLE";

	@Override
	protected int getLayoutId() {
		return R.layout.mine_red;
	}

	@Override
	protected void init() {
		yearMonth = getCurrentDate();
		if (!TextUtils.isEmpty(yearMonth)) {
			str = yearMonth.split("-");
		}
		String title = getIntent().getStringExtra(BILL_TITLE);
		StyleUtil.init(this, title, "筛选", new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog();
			}
		});
		fragment1 = MineBillFragment.newInstance("0");
		fragment4 = MineBillFragment.newInstance("1");
		fragment2 = MineBillFragment.newInstance("2");
		fragment3 = MineBillFragment.newInstance("3");

		fragmentList = new ArrayList<>();
		List<String> tabsTitle = new ArrayList<>();
		fragmentList.add(fragment1);
		tabsTitle.add("全部");
		fragmentList.add(fragment4);
		tabsTitle.add("充值");
		fragmentList.add(fragment2);
		tabsTitle.add("消费");
		fragmentList.add(fragment3);
		tabsTitle.add("退款");
		viewPager.setOffscreenPageLimit(3);

		viewPager.setAdapter(new CustomFragmentPagerAdapter(getSupportFragmentManager(), tabsTitle, fragmentList));

		tabLayout.setupWithViewPager(viewPager);
		tabLayout.setTabMode(TabLayout.MODE_FIXED);
		tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
	}

	private void showDialog() {
		BirthdayDateTimePickerDialog2 dialog;
		if (monthStr != null && !monthStr.equalsIgnoreCase("")) {
			String[] str_temp = monthStr.split("-");
			dialog = new BirthdayDateTimePickerDialog2(this, Integer.valueOf(str_temp[0]),
					Integer.valueOf(str_temp[1]));
		} else
			dialog = new BirthdayDateTimePickerDialog2(this, Integer.valueOf(str[0]),
				Integer.valueOf(str[1]));
		/*
		 * 实现接口
		 */
		dialog.setOnDateTimeSetListener(new BirthdayDateTimePickerDialog2.OnBirthDateTimeSetListener() {

			@Override
			public void OnDateTimeSet(AlertDialog dialog, int year, int month, int day) {
				if (Integer.valueOf(str[0]) == year && Integer.valueOf(str[1]) < month) {
					ToastUtil.show(mContext, "选择的日期应小于当前日期");
					return;
				}
				if (month < 10) {
					yearMonth = year + "-0" + month + "";
				} else {
					yearMonth = year + "-" + month + "";
				}
				monthStr = yearMonth;

				// 更新3个列表
				fragment1.loadData(true);
				fragment2.loadData(true);
				fragment3.loadData(true);
			}
		});
		dialog.show();
	}

	// 年月
	private String[] str;
	private String yearMonth;
	private String monthStr = "";

	public String getCurrentDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM", Locale.CHINA);
		return sdf.format(date);
	}

	public String getMonthStr() {
		return monthStr;
	}
}
